package com.micromsg.basic.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.micromsg.basic.msg.vo.AllFriendVO;
import com.micromsg.basic.sys.dao.UserMapper;
import com.micromsg.basic.sys.model.User;
import com.micromsg.basic.sys.service.UserService;
import com.micromsg.basic.sys.vo.entity.UserEntityVO;
import com.micromsg.basic.sys.vo.request.UserRequestVO;
import com.micromsg.basic.sys.vo.response.UserResponseVO;
import com.micromsg.common.constant.CommonConstant;
import com.micromsg.common.core.PageResult;
import com.micromsg.common.service.AbstractService;
import com.micromsg.common.util.IdGen;
import com.micromsg.common.util.ListUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;

/**
 * 用户表
 *
 * @author yangyang
 * @date 2024/04/06
 **/
@Slf4j
@Service
@Transactional
public class UserServiceImpl extends AbstractService<User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 获取用户表分页列表
     *
     * @param userRequestVO 用户表查询条件
     * @return pageResult 用户表分页结果
     */
    @Override
    public PageResult<UserResponseVO> queryUserPageList(UserRequestVO userRequestVO) {
        //
        if (userRequestVO.getPageNumber() == null) userRequestVO.setPageNumber(CommonConstant.DEFAULT_PAGENUMBER);
        if (userRequestVO.getPageSize() == null) userRequestVO.setPageSize(CommonConstant.DEFAULT_PAGESIZE);
        //
        List<UserResponseVO> userResponseVOList = Collections.emptyList();
        Condition condition = new Condition(User.class);
        Example.Criteria criteria = condition.createCriteria();
        //
        //if (StringUtils.isNotBlank(userRequestVO.getDesc())) {
        //    criteria.andLike("userName", "%" + userRequestVO.getDesc() + "%");
        //}
        //排序
        condition.orderBy("createDate").desc();
        //
        int total = this.selectCountByCondition(condition);
        if (total > 0) {
            PageHelper.startPage(userRequestVO.getPageNumber(), userRequestVO.getPageSize());
            List<User> userList = this.selectByCondition(condition);
            userResponseVOList = ListUtil.convertList(userList, UserResponseVO.class);
        }
        //
        return PageResult.of(total, userResponseVOList);
    }

    @Override
    public AllFriendVO getAllFriend(String userId) {
        List<User> allFriend = userMapper.getAllFriend(userId);
        for (User u : allFriend) {
            u.setAvatar("http://127.0.0.1:9000/msg/AVATAR/" + u.getAvatar());
        }
        List<UserEntityVO> voList = ListUtil.convertList(allFriend, UserEntityVO.class);
        AllFriendVO vo = new AllFriendVO();
        vo.setAllFriend(voList);
        return vo;
    }

    /**
     * 获取用户表详细信息
     *
     * @param id 用户表ID
     * @return 用户表
     */
    @Override
    public UserEntityVO getUserEntityVO(String id) {
        User user = this.selectByPrimaryKey(id);
        if (null == user) return null;

        UserEntityVO userEntityVO = new UserEntityVO();
        BeanUtils.copyProperties(user, userEntityVO);
        return userEntityVO;
    }

    /**
     * 添加用户表
     *
     * @param userEntityVO 用户表
     */
    @Override
    public void addUser(UserEntityVO userEntityVO) {
        User user = new User();
        BeanUtils.copyProperties(userEntityVO, user);
        user.setId(IdGen.next());
        this.insertSelective(user);
    }

    /**
     * 修改用户表
     *
     * @param userEntityVO 用户表
     */
    @Override
    public void updateUser(UserEntityVO userEntityVO) {
        User user = new User();
        BeanUtils.copyProperties(userEntityVO, user);
        this.updateByPrimaryKeySelective(user);
    }


    /**
     * 删除用户表
     *
     * @param id 用户表ID
     */
    @Override
    public void deleteUser(String id) {
        this.deleteByPrimaryKey(id);
    }
}
